package com.swufe.scoresforapp;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.gson.Gson;
import com.swufe.scoresforapp.Yonghu.Dataimport;
import com.swufe.scoresforapp.adapter.CompareAdapter;
import com.swufe.scoresforapp.bean.CompareModel;
import com.swufe.scoresforapp.bean.SchoolBean;

import java.util.ArrayList;
import java.util.List;

public class DeleteActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private LinearLayoutManager linearLayoutManager;
    private Context context;
    private List<CompareModel> list = new ArrayList<>();
    private CompareAdapter myadapter;
    private Button confirm;
    private Button collect;
    private List<SchoolBean> mSchool;
    private ArrayList<String> name;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.compare_content);
        confirm = findViewById(R.id.confirm);
        collect = findViewById(R.id.collect_compare);
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //点击跳转至对比详情页面
                Intent intent = new Intent(context, CompareActivity.class);
                intent.setClass(context, CompareActivity.class);
                intent.putExtra("information", new Gson().toJson(mSchool));
                context.startActivity(intent);
            }
        });
        collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(DeleteActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
                //将数据传入收藏对比界面
            }
        });
        initData();
        initView();
    }

    //获取传入对比表的数据
    public void setData(){
        new Thread(
            new Runnable() {
                @Override
                public void run() {
                    mSchool = new ArrayList<>();
                    Dataimport in = new Dataimport();
                    for (int i= 0;i<4;i++){
                        if(name.get(i)==null){
                            mSchool.add(new SchoolBean(null,"","",
                                    "","","","","","",
                                    "","",""));
                        }else {
                            mSchool.add(in.getSchool(name.get(i)));
                        }
                    }
                }
            }
    ).start();}
    public void initData(){
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        name = (ArrayList<String>) bundle.getSerializable("newSchool");

    }//传入数据 现在的for循环只是为了测试用

    public void initView(){
        mRecyclerView = findViewById(R.id.compare_recyclerview);
        linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        myadapter = new CompareAdapter(this,name);
        myadapter.setItemClickListener(new CompareAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                //删除选中的对比学校
                Toast.makeText(DeleteActivity.this, "此学校已成功删除", Toast.LENGTH_SHORT).show();
                list.remove(position);
                myadapter.notifyItemRemoved(position);
                Log.i("1111","close:");
            }
        });
        mRecyclerView.setAdapter(myadapter);
    }
}